import { _decorator, Component, Node } from 'cc';
import { EnDynamicLineType } from '../../Common/const/EnCommon';
import { EnEvent } from '../../Common/const/EnEvent';
import { GameAPI } from '../../Common/GameAPI';
import { TowerController } from './TowerController';
const { ccclass, property } = _decorator;

export class TowerInfo {
    public towerNode: Node = null;
    public level: number = 0;
    public active: boolean = false;
    public index: number = 0;
}

@ccclass('TowerManager')
export class TowerManager extends Component {

    @property(Node)
    private tower1Node: Node = null;
    @property(Node)
    private tower2Node: Node = null;
    @property(Node)
    private tower3Node: Node = null;
    @property(Node)
    private tower4Node: Node = null;
    @property(Node)
    private heroNode: Node = null;
    @property(Node)
    private mainTowerNode: Node = null;

    @property(Node)
    private towerParentNode: Node = null;

    private towerInfoData: Array<TowerInfo> = [];
    private curIndex: number = 0;

    private checkTime: number = 1;
    private towerNodeList: Array<Node> = [];

    start() {
        this.towerNodeList.push(this.tower1Node);
        this.towerNodeList.push(this.tower2Node);
        this.towerNodeList.push(this.tower3Node);
        this.towerNodeList.push(this.tower4Node);
        for (let index = 0; index < this.towerNodeList.length; index++) {
            // const element = this.towerParentNode.getChildByName("Tower" + index);
            const element = this.towerNodeList[index];
            let info = new TowerInfo();
            info.active = false;
            info.index = index + 1;
            info.towerNode = element;
            info.level = 0;
            let controller: TowerController = element.getComponent(TowerController);
            controller.OnInit(this, index + 1);
            this.towerInfoData.push(info);
        }
    }


    update(deltaTime: number) {
        this.checkTime -= deltaTime;
        if (this.checkTime <= 0) {
            this.checkTime = 1;
            this.checkGou();
        }
    }

    checkGou() {
        for (let index = 0; index < this.towerNodeList.length; index++) {
            const element = this.towerNodeList[index];
            if (element.active) {
                let tower = element.getComponent(TowerController);
                if (tower.GetHowManyNum() >= 0) {
                    GameAPI.FireGEvent(EnEvent.DynamicLineEvent, { type: EnDynamicLineType.UpgradeTower });
                    return;
                }
            }
        }
    }

    GetMinLevelTower() {
        let minLevel = 100;
        let towerIndex = 0;
        for (let index = 0; index < this.towerInfoData.length; index++) {
            const element = this.towerInfoData[index];
            if (minLevel > element.level) {
                minLevel = element.level;
                towerIndex = index;
            }
        }
        return this.towerInfoData[towerIndex];
    }

    UpdateLevel(id: number, level: number) {
        for (let index = 0; index < this.towerInfoData.length; index++) {
            const element = this.towerInfoData[index];
            if (element.index == id) {
                this.towerInfoData[index].level = level;
                if (level == 1) {
                    let controller: TowerController = element.towerNode.getComponent(TowerController);
                    controller.heroTowerNode.active = true;
                }
                break;
            }
        }
        let upgradeTipNum = 0;
        for (let index = 0; index < this.towerInfoData.length; index++) {
            const element = this.towerInfoData[index];
            if (element.level == 1) {
                upgradeTipNum++;
            }
        }
        if (upgradeTipNum == 4) {
            GameAPI.FireGEvent(EnEvent.GuideTipEvent, { index: 5, active: true });
        }
        let info: TowerInfo = this.GetMinLevelTower();
        let minLevel: number = info.level;
        if (minLevel <= 0) {
            let controller: TowerController = info.towerNode.getComponent(TowerController);
            controller.noneTower.active = true;
            controller.numNode.active = true;
            let levelNum = 0;
            for (let index = 0; index < this.towerInfoData.length; index++) {
                const element = this.towerInfoData[index];
                if (element.level == 0) {
                    levelNum = levelNum + 1;
                }
            }
            if (levelNum == 0) {
                minLevel = 1;
            }
            else {
                return;
            }
        }
        for (let index = 0; index < this.towerInfoData.length; index++) {
            const element = this.towerInfoData[index];
            if (element.level == minLevel) {
                let controller: TowerController = element.towerNode.getComponent(TowerController);
                controller.noneTower.active = true;
                controller.numNode.active = true;
            }
        }
    }

}


